package main

import (
	"fmt"
	"google.golang.org/grpc"
	"Gocode/study.com/RPCandgRPC/grpc/serverstream/model"
	"net"
)

type Person struct {
}

func (u *Person) GetUserInfo(request *model.UserRequest, stream model.GetUserInfoService_GetUserInfoServer) error {
	user := &model.User{Name: request.Name, Age: request.Age, Gender: model.Gender(request.Gender)}
	//for i := 0; i < reflect.ValueOf(user).Elem().NumField(); i++ {
	//fieldNames := reflect.TypeOf(user).Elem().Field(i)
	//fieldName := fieldNames.Name
	stream.Send(user)
	//}
	return nil
}

func main() {
	//初始化为指针类型
	person := &Person{}
	//将服务器对象注册
	server := grpc.NewServer()
	//models.RegisterGetUserInfoServiceServer(server, reflect.ValueOf(person).Interface().(models.GetUserInfoServiceServer))
	model.RegisterGetUserInfoServiceServer(server, person)

	//监听
	listen, err := net.Listen("tcp", ":9000")
	if err != nil {
		fmt.Println("net.Listen error=", err)
		//return
	}
	// 开启服务
	server.Serve(listen)

}
